const mongoose = require('mongoose');
let excel = require('./admin.channel.excel');
let clickCensus = require('./admin.clickCensus');

module.exports={
    async list(options){
        let FriendLinkClickModel=mongoose.models['FriendLinkClick'];
        let friendLinkClicks=await FriendLinkClickModel.find({addDate:options.addDate});
        friendLinkClicks.sort(function (param1, param2) {
                return (param1.sceneName).localeCompare(param2.sceneName,"zh");
            }
        );
        await clickCensus.checkData(options.addDate);
        let data = await clickCensus.findDataByAddDate(options.addDate);
        return {friendLinkClicks, data};
    },
    async detailListExcel(options){
        let FriendLinkClickModel = mongoose.models['FriendLinkClick'];
        let list = await FriendLinkClickModel.find({addDate:options.addDate});
        list.sort(
            function compareFunction(param1, param2) {
                return (param1.sceneName).localeCompare(param2.sceneName,"zh");
            }
        );
        let data = await excel.detailListExcel(list);
        return data;

    },
    async totalListExcel(options){
        let FriendLinkClickModel = mongoose.models['FriendLinkClick'];
        if(options.addDate != new Date().toLocaleDateString()){
            let data = await clickCensus.findDataByAddDate(options.addDate);
            return await excel.totalListExcel(data, options.addDate);
        }
        let data = await FriendLinkClickModel.aggregate([
            {
                $match: {
                    addDate: options.addDate
                }
            },
            {
                $group: {
                    _id: '$appName',
                    count:{$sum: '$count'},
                    newCount:{$sum: '$newCount'},
                    openCount:{$sum: '$openCount'},
                    newOpenCount:{$sum: '$newOpenCount'},
                }
            }
        ]);
        if(data && data.length){
            data.sort(
                function compareFunction(param1, param2) {
                    return (param1._id).localeCompare(param2._id,"zh");
                }
            );
        }
        // console.log('totalListExcel data =', data);
        return await excel.totalListExcel(data, options.addDate);
    }
};